package server.controller;

import java.util.Collection;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;

import server.LineageClient;


import l1j.server.server.model.Broadcaster;
import l1j.server.server.model.L1World;
import l1j.server.server.model.Instance.L1DollInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_DoActionGFX;

public class DollobserverController extends TimerTask{
	private static Logger _log = Logger.getLogger(DollobserverController.class.getName());
	Random rnd = new Random(System.nanoTime());
	private final int _sendgfxtime;
	private static Timer _dollobserverTimer = new Timer();
	public DollobserverController(int sendgfxtime) { _sendgfxtime = sendgfxtime; }

	public void start() {
		_dollobserverTimer.scheduleAtFixedRate(DollobserverController.this, 0, _sendgfxtime);
	}

	@Override
	public void run() {
		try {
			Collection<L1PcInstance> _list = null;
			_list = L1World.getInstance().getAllPlayers();
			for(L1PcInstance _client : _list){
				if(_client == null){
					continue;
				} else{
					for (L1DollInstance doll : _client.getDollList().values()) {
						_client.sendPackets(new S_DoActionGFX(doll.getId(), 66 + rnd.nextInt(2)));
						Broadcaster.broadcastPacket(_client, new S_DoActionGFX(doll.getId(), 66 + rnd.nextInt(2)));
					}
				}
			}
			_list = null;
		} catch (Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
			cancel();
		}
	}
}
